/*
 * @Author: lp 654528967@qq.com
 * @Date: 2022-01-24 11:53:19
 * @LastEditors: lp 654528967@qq.com
 * @LastEditTime: 2022-05-18 10:57:09
 * @FilePath: \nien-vue3-admin\src\directive\hasPermi.ts
 * @Description: 权限指令，分为按钮（默认），switch，输入框
 */

import { BindingTypes } from '@vue/compiler-core'
import { VNode } from 'vue'
import useUser from '@/store/modules/user'

class Permi {
  el:HTMLElement = null

  binding:BindingTypes = null

  vnode:VNode = null

  pervVnode:VNode = null

  hasPermi:boolean = null

  constructor(arg) {
    const [el, binding, vnode, pervVnode] = arg
    this.el = el
    this.binding = binding
    this.vnode = vnode
    this.pervVnode = pervVnode
    const permissions = useUser().permissions
    this.hasPermi = permissions.some(el => el === '*:*:*' || binding.value.some(permi => permi === el))
    this.init(binding.arg)
  }

  init(arg) {
    if (!this.hasPermi) {
      switch (arg) {
        case undefined:
          this.btnPermi()
          break
        case 'switch':
          this.switchPermi()
          break
        case 'input':
          this.inputPermi()
      }
    }
  }

  btnPermi() {
    const $parent = this.el.parentElement
    $parent && $parent.removeChild(this.el)
  }

  switchPermi() {
    console.log('switch')
  }

  inputPermi() {}
}

const permi = {
  mounted(...args) {
    new Permi(args)
  },
}
export default permi